package com.glsc.ngateway.amdoc.controller;

import com.github.pagehelper.PageInfo;
import com.glsc.ngateway.amdoc.pojo.*;
import com.glsc.ngateway.amdoc.services.IAssetProductService;
import com.glsc.ngateway.common.api.amdoc.dto.*;
import com.glsc.ngateway.common.api.common.dto.PageDataResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @Title: FlowWorkController
 * @Description: Controller
 * @author: 张宁
 * @version: 1.0.0
 * @date: 2020/12/21 16:29
 */


@RestController
@RequestMapping("assetProduct")
public class AssetProductController {


    @Autowired
    private IAssetProductService assetProductService;


    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @RequestMapping("getAssetProductList")
    @ResponseBody
    public PageDataResult<AssetProductDto> getAssetProductList(@RequestParam("pageNum") Integer pageNum,
                                                               @RequestParam("pageSize") Integer pageSize,
                                                               @RequestParam(value = "productCode", defaultValue = "", required = false) String productCode,
                                                               @RequestParam(value = "product", defaultValue = "", required = false) String product,
                                                               @RequestParam(value = "productNature", defaultValue = "", required = false) String productNature,
                                                               @RequestParam(value = "productSysId", defaultValue = "", required = false) Integer productSysId) {
        logger.info("获取资管产品列表");
        PageInfo<AssetProduct> pageinfo = assetProductService.getAssetProductList(pageNum, pageSize, productCode, product, productNature, productSysId);
        return new PageDataResult<>(pageinfo.getPageNum(), pageinfo.getPageSize(), pageinfo.getList().stream()
                .map(domain -> {
                    AssetProductDto dto = new AssetProductDto();
                    BeanUtils.copyProperties(domain, dto);
                    return dto;
                })
                .collect(Collectors.toList()));
    }

    @RequestMapping("getAssetProductStaffListBySysId")
    @ResponseBody
    public PageDataResult<AssetProductMemberDto> getAssetProductStaffListBySysId(@RequestParam("pageNum") Integer pageNum,
                                                                                 @RequestParam("pageSize") Integer pageSize,
                                                                                 @RequestParam("productSysId") Integer productSysId) {
        logger.info("获取资管产品员工列表");
        PageInfo<AssetProductMember> pageinfo = assetProductService.getAssetProductStaffListBySysId(pageNum, pageSize, productSysId);
        return new PageDataResult<>(pageinfo.getPageNum(), pageinfo.getPageSize(), pageinfo.getList().stream()
                .map(domain -> {
                    AssetProductMemberDto dto = new AssetProductMemberDto();
                    BeanUtils.copyProperties(domain, dto);
                    return dto;
                })
                .collect(Collectors.toList()));
    }


    @RequestMapping("getAssetProductWorkFlowListBySysId")
    @ResponseBody
    public PageDataResult<AssetProductWorkFlowDto> getAssetProductWorkFlowListBySysId(@RequestParam("pageNum") Integer pageNum,
                                                                                      @RequestParam("pageSize") Integer pageSize,
                                                                                      @RequestParam("productSysId") Integer productSysId,
                                                                                      @RequestParam(value = "workflowIdList", required = false) List<Integer> workflowIdList,
                                                                                      @RequestParam(value = "requestName", required = false) String requestName) {
        logger.info("资管产品OA流程关联信息");
        PageInfo<AssetProductWorkFlow> pageinfo = assetProductService.getAssetProductWorkFlowListBySysId(pageNum, pageSize, productSysId, workflowIdList, requestName);
        return new PageDataResult<>(pageinfo.getPageNum(), pageinfo.getPageSize(), pageinfo.getList().stream()
                .map(domain -> {
                    AssetProductWorkFlowDto dto = new AssetProductWorkFlowDto();
                    BeanUtils.copyProperties(domain, dto);
                    return dto;
                })
                .collect(Collectors.toList()));
    }


    @RequestMapping("getAssetProductDocDirectoryListBySysId")
    @ResponseBody
    public PageDataResult<AssetProductDocDirectoryDto> getAssetProductDocDirectoryListBySysId(@RequestParam("pageNum") Integer pageNum,
                                                                                              @RequestParam("pageSize") Integer pageSize,
                                                                                              @RequestParam("productSysId") Integer productSysId) {
        logger.info("获取资管产品档案目录列表");
        PageInfo<AssetProductDocDirectory> pageinfo = assetProductService.getAssetProductDocDirectoryListBySysId(pageNum, pageSize, productSysId);
        return new PageDataResult<>(pageinfo.getPageNum(), pageinfo.getPageSize(), pageinfo.getList().stream()
                .map(domain -> {
                    AssetProductDocDirectoryDto dto = new AssetProductDocDirectoryDto();
                    BeanUtils.copyProperties(domain, dto);
                    return dto;
                })
                .collect(Collectors.toList()));
    }


    @RequestMapping("getAssetProductDirTemplate")
    @ResponseBody
    public PageDataResult<AssetProductDirTempDto> getAssetProductDirTemplate(@RequestParam("pageNum") Integer pageNum,
                                                                             @RequestParam("pageSize") Integer pageSize,
                                                                             @RequestParam("productNature") String productNature) {
        logger.info("获取资管产品目录模板清单");
        PageInfo<AssetProductDirTemp> pageinfo = assetProductService.getAssetProductDirTemplate(pageNum, pageSize, productNature);
        return new PageDataResult<>(pageinfo.getPageNum(), pageinfo.getPageSize(), pageinfo.getList().stream()
                .map(domain -> {
                    AssetProductDirTempDto dto = new AssetProductDirTempDto();
                    BeanUtils.copyProperties(domain, dto);
                    return dto;
                })
                .collect(Collectors.toList()));
    }

    @RequestMapping("getAssetProductDocListBySysId")
    @ResponseBody
    public PageDataResult<AssetProductDocDto> getAssetProductDocListBySysId(@RequestParam("pageNum") Integer pageNum,
                                                                            @RequestParam("pageSize") Integer pageSize,
                                                                            @RequestParam("productSysId") Integer productSysId,
                                                                            @RequestParam(value = "directory", required = false) Integer directory,
                                                                            @RequestParam(value = "document", required = false) String document) {
        logger.info("获取资管产品档案列表");
        PageInfo<AssetProductDoc> pageinfo = assetProductService.getAssetProductDocListBySysId(pageNum, pageSize, productSysId, directory, document);
        return new PageDataResult<>(pageinfo.getPageNum(), pageinfo.getPageSize(), pageinfo.getList().stream()
                .map(domain -> {
                    AssetProductDocDto dto = new AssetProductDocDto();
                    BeanUtils.copyProperties(domain, dto);
                    return dto;
                })
                .collect(Collectors.toList()));
    }

    @RequestMapping(value = "getAssetProductDocListComprehensive", method = RequestMethod.POST)
    @ResponseBody
    public PageDataResult<AssetProductDocComprehensiveDto> getAssetProductDocListComprehensive(@RequestBody AssetProductRequestComprehensiveDto requestDto) {
        logger.info("获取资管产品档案列表");
        PageInfo<AssetProductDocComprehensive> pageinfo = assetProductService.getAssetProductDocListComprehensive(requestDto);
        PageDataResult<AssetProductDocComprehensiveDto> page = new PageDataResult<>();
        page.setList(pageinfo.getList().stream().map(domain -> {
            AssetProductDocComprehensiveDto dto = new AssetProductDocComprehensiveDto();
            BeanUtils.copyProperties(domain, dto);
            return dto;
        }).collect(Collectors.toList()));
        page.setTotals(Math.toIntExact(pageinfo.getTotal()));
        return page;
    }

    @RequestMapping(value = "/getOaWorkflowRequestPage", method = RequestMethod.GET)
    public PageDataResult<AssetFlow> getOaWorkflowRequestPage(@RequestParam("pageNum") Integer pageNum,
                                                              @RequestParam("pageSize") Integer pageSize,
                                                              @RequestParam(value = "workflowIdList", required = false) List<Integer> workflowIdList,
                                                              @RequestParam(value = "requestName", required = false) String requestName,
                                                              @RequestParam(value = "currentNodeIdList", required = false) List<String> currentNodeIdList) {

        logger.info("获取资管OA流程数据");
        return new PageDataResult<>(assetProductService.getOaWorkflowRequestPage(pageNum, pageSize, workflowIdList, requestName, currentNodeIdList));
    }

    @RequestMapping("getOaWorkflowInfoByRequestId")
    @ResponseBody
    public AssetFlowDto getOaWorkflowInfoByRequestId(@RequestParam("requestId") Integer requestId) {
        logger.info("获取OA流程信息={}", requestId);
        AssetFlowDto dto = new AssetFlowDto();
        BeanUtils.copyProperties(assetProductService.getOaWorkflowInfoByRequestId(requestId), dto);
        return dto;
    }

    @RequestMapping("getAssetProductSaleAgencyList")
    @ResponseBody
    public PageDataResult<AssetProductSaleAgencyDto> getAssetProductSaleAgencyList(@RequestParam("pageNum") Integer pageNum,
                                                                                   @RequestParam("pageSize") Integer pageSize) {
        logger.info("获取代销机构列表");
        PageInfo<AssetProductSaleAgency> pageinfo = assetProductService.getAssetProductSaleAgencyList(pageNum, pageSize);
        return new PageDataResult<>(pageinfo.getPageNum(), pageinfo.getPageSize(), pageinfo.getList().stream()
                .map(domain -> {
                    AssetProductSaleAgencyDto dto = new AssetProductSaleAgencyDto();
                    BeanUtils.copyProperties(domain, dto);
                    return dto;
                })
                .collect(Collectors.toList()));
    }

    @RequestMapping("getAssetProductDefaultMemberList")
    @ResponseBody
    public PageDataResult<AssetProductDefaultMemberDto> getAssetProductDefaultMemberList(@RequestParam("pageNum") Integer pageNum,
                                                                                         @RequestParam("pageSize") Integer pageSize) {
        logger.info("获取默认项目成员列表");
        PageInfo<AssetProductDefaultMember> pageinfo = assetProductService.getAssetProductDefaultMemberList(pageNum, pageSize);
        return new PageDataResult<>(pageinfo.getPageNum(), pageinfo.getPageSize(), pageinfo.getList().stream()
                .map(domain -> {
                    AssetProductDefaultMemberDto dto = new AssetProductDefaultMemberDto();
                    BeanUtils.copyProperties(domain, dto);
                    return dto;
                })
                .collect(Collectors.toList()));
    }

    @RequestMapping("getInvestmentManagerInfo")
    @ResponseBody
    public List<InvestmentManagerDto> getInvestmentManagerInfo(@RequestParam("id") List<Integer> id) {
        logger.info("获取投资经理信息");
        if (id == null) {
            return null;
        }
        if (id != null && id.size() > 50) {
            return null;
        }
        return assetProductService.getInvestmentManagerInfo(id);
    }

    @RequestMapping("getAssetViewByName")
    @ResponseBody
    public PageDataResult getAssetViewByName(@RequestParam("pageNum") Integer pageNum,
                                             @RequestParam("pageSize") Integer pageSize,
                                             @RequestParam("view") String view,
                                             @RequestParam(value = "productCode", defaultValue = "", required = false) String productCode,
                                             @RequestParam(value = "product", defaultValue = "", required = false) String product,
                                             @RequestParam(value = "productNature", defaultValue = "", required = false) String productNature,
                                             @RequestParam(value = "productSysId", defaultValue = "", required = false) Integer productSysId) {
        logger.info("获取资管产品视图：" + view);
        if (view.equals("资管产品列表")) {
            return new PageDataResult<>(assetProductService.getAssetProductList(pageNum, pageSize, productCode, product, productNature, productSysId));
        } else if (view.equals("资管产品项目组成员")) {
            return new PageDataResult<>(assetProductService.getAssetProductStaffListBySysId(pageNum, pageSize, productSysId));
        } else if (view.equals("资管产品档案目录列表")) {
            return new PageDataResult<>(assetProductService.getAssetProductDocDirectoryListBySysId(pageNum, pageSize, productSysId));
        } else if (view.equals("资管产品档案列表")) {
            return new PageDataResult<>(assetProductService.getAssetProductDocListBySysId(pageNum, pageSize, productSysId, null, null));
        } else if (view.equals("资管产品档案模板清单")) {
            return new PageDataResult<>(assetProductService.getAssetProductDirTemplate(pageNum, pageSize, productNature));
        } else {
            // return new PageDataResult(assetProductService.getAssetProductWorkFlowListBySysId(pageNum, pageSize, productSysId));
            return null;
        }
    }

    @RequestMapping("getAssetProductWorkFlowListView")
    @ResponseBody
    public PageDataResult<AssetProductWorkFlowListViewDto> getAssetProductWorkFlowListView(@RequestBody AssetProductWorkFlowListViewDto assetProductWorkFlowListViewDto) {
        logger.info("获取资管产品工作流视图");
        PageInfo<AssetProductWorkFlowListView> pageinfo = assetProductService.getAssetProductWorkFlowListView(assetProductWorkFlowListViewDto.getPageNum(), assetProductWorkFlowListViewDto.getPageSize(), assetProductWorkFlowListViewDto);
        PageDataResult<AssetProductWorkFlowListViewDto> page = new PageDataResult<>();
        page.setList(pageinfo.getList().stream().map(domain -> {
            AssetProductWorkFlowListViewDto dto = new AssetProductWorkFlowListViewDto();
            BeanUtils.copyProperties(domain, dto);
            return dto;
        }).collect(Collectors.toList()));
        page.setTotals(Math.toIntExact(pageinfo.getTotal()));
        return page;
    }

    @RequestMapping("getAssetProductRequestNodeListView")
    @ResponseBody
    public PageDataResult<AssetProductRequestNodeListViewVo> getAssetProductRequestNodeListView(@RequestBody AssetProductRequestNodeListViewDto assetProductRequestNodeListViewDto) {
        logger.info("获取资管产品流程节点任务列表视图");
        if (assetProductRequestNodeListViewDto == null || CollectionUtils.isEmpty(assetProductRequestNodeListViewDto.getRequestIdList())) {
            throw new IllegalArgumentException("requestIdList不能为空");
        }
        PageInfo<AssetProductRequestNodeListView> pageinfo = assetProductService.getAssetProductRequestNodeListView(
                assetProductRequestNodeListViewDto.getPageNum(), assetProductRequestNodeListViewDto.getPageSize(), assetProductRequestNodeListViewDto);
        return new PageDataResult<>(pageinfo.getPageNum(), pageinfo.getPageSize(), pageinfo.getList().stream()
                .map(domain -> {
                    AssetProductRequestNodeListViewVo dto = new AssetProductRequestNodeListViewVo();
                    BeanUtils.copyProperties(domain, dto);
                    return dto;
                })
                .collect(Collectors.toList()));
    }

    @RequestMapping(value = "createProduct")
    public String createProduct(@RequestBody String productInfo) {
        logger.info("调用OA-Webservice服务createProduct开始,请求参数为：{}", productInfo);
        String oaResponse = assetProductService.createProduct(productInfo);
        logger.info("调用OA-Webservice服务createProduct完成,OA返回结果为：\r\n{}", oaResponse);
        return oaResponse;
    }


    @RequestMapping(value = "updateProduct")
    public String updateProduct(@RequestBody String productInfo) {
        logger.info("调用OA-Webservice服务updateProduct开始,请求参数为：{}", productInfo);
        String oaResponse = assetProductService.updateProduct(productInfo);
        logger.info("调用OA-Webservice服务updateProduct完成,OA返回结果为：\r\n{}", oaResponse);
        return oaResponse;
    }


    @RequestMapping(value = "batupload")
    public String batupload(@RequestBody String productInfo) {
        logger.info("调用OA-Webservice服务batupload开始,请求参数为：{}", productInfo);
        String oaResponse = assetProductService.batupload(productInfo);
        logger.info("调用OA-Webservice服务batupload完成,OA返回结果为：\r\n{}", oaResponse);
        return oaResponse;
    }


}
